package com.sd.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sd.coupon.entity.Goods;
import com.sd.coupon.mapper.GoodsMapper;
import com.sd.coupon.model.req.GoodsPageReq;
import com.sd.coupon.model.req.GoodsReq;
import com.sd.coupon.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lws
 * @since 2019-01-22
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Override
    public IPage<Goods> getGoodsPage(GoodsPageReq req){

        if(req==null){
            req = new GoodsPageReq();
            req.setCurrent(1L);
            req.setSize(10L);
        }

        QueryWrapper<Goods> query = buildPageQueryWrapper(req);
        IPage res = super.page(new Page<Goods>(req.getCurrent(), req.getSize()), query);

        return res;
    }

    private QueryWrapper<Goods> buildPageQueryWrapper(GoodsPageReq req){

        QueryWrapper<Goods> query = new QueryWrapper<Goods>();
        if(req == null){
            return query;
        }

        //根据种类查询商品
        if(req.getCid()!=null){
            query.eq("cate_id",req.getCid());
        }

        //根据关键词查询商品
        if(!StringUtils.isEmpty(req.getKeyWord())){
            query.like("name",req.getKeyWord());
        }

        //排序
        if(!StringUtils.isEmpty(req.getSort())){
//            query.orderByDesc(strings);
        }
        return query;
    }

    @Override
    public Goods getGoods(GoodsReq req){
        QueryWrapper<Goods> query = new QueryWrapper<>();
        if(req!=null){
            Long numIid = req.getNumIid();
            if(numIid!=null){
                query.eq("num_iid",req.getNumIid());
            }
        }
        Goods res = this.getOne(query);
        return res;
    }

}
